package com.woniu.tms.web.listener;

import com.woniu.tms.manage.utils.MyUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Configuration
@EnableScheduling
public class RegularBackup {

    @Scheduled(cron = "0 0 11 * * ?")
    public void backup() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String filePath = request.getSession().getServletContext().getRealPath("datas");
        String fileName = MyUtils.getDay();
        try {
            MyUtils.exportDatabaseTool("172.16.13.13", "root", "123456", filePath, fileName, "tms");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
